import { Component, OnInit, Input } from '@angular/core';
import { AppGlobalService } from '../../shared/service/app-global.service';
import { HttpService } from 'src/app/shared/service/http.service';

@Component({
    selector: 'app-recommend-data',
    templateUrl: './recommend-data.component.html',
    styleUrls: ['./recommend-data.component.scss'],
})
export class RecommendDataComponent implements OnInit {
    @Input() tagData: any;
    // 筛选及页面数据参数
    params: any = {}; // 需要传的参数
    // 当前页数，默认1
    pageNo = 1;
    sessionId: any;
    constructor(private appGlobalService: AppGlobalService, private http: HttpService) { }

    ngOnInit() {
        console.log(this.tagData)
        console.log(this.tagData.data.data)
        /*  this.tagData.data.data=[
           {
               "matchtimes":"2019年12月12日",
               "times":"",
               "link":{
                   "type":"page",
                   "params":{
                       "opportunity_id":"5e1d87d2367a22000172c95c",
                       "customer_id":"5dce1d445eddff0001001265"
                   },
                   "target":"142855"
               },
               "title":{
                   "label":"嗯嗯找深圳餐馆",
                   "isAuthenticated":{
                       "label":"认证",
                       "value":false
                   }
               },
               "rent":"-元/月",
               "points":"12-21㎡",
               "info":"餐馆 | 深圳",
               "status":{
                   "label":"未跟进"
               }
           },
           {
               "matchtimes":"2019年12月12日",
               "times":"19-08-09",
               "link":{
                   "type":"page",
                   "params":{
                       "opportunity_id":"5d4b8fe8d6389d000139438f",
                       "customer_id":"5d4b8d6bd6389d000139438c"
                   },
                   "target":"142855"
               },
               "title":{
                   "label":"测试业务员找罗湖区餐馆",
                   "isAuthenticated":{
                       "label":"认证",
                       "value":false
                   }
               },
               "rent":"2000-3000元/月",
               "points":"10-15㎡",
               "info":"餐馆 | 罗湖区",
               "status":{
                   "label":"未跟进"
               }
           },
           {
               "matchtimes":"2019年12月12日",
               "times":"",
               "link":{
                   "type":"page",
                   "params":{
                       "opportunity_id":"5e1e6aafafcdcb000117ad79",
                       "customer_id":"5e1e6aafafcdcb000117ad78"
                   },
                   "target":"142855"
               },
               "title":{
                   "label":"孙老板找深圳餐馆",
                   "isAuthenticated":{
                       "label":"认证",
                       "value":false
                   }
               },
               "rent":"-元/月",
               "points":"10-98㎡",
               "info":"餐馆 | 深圳",
               "status":{
                   "label":"未跟进"
               }
           },
           {
               "matchtimes":"2019年12月11日",
               "times":"19-08-02",
               "link":{
                   "type":"page",
                   "params":{
                       "opportunity_id":"5d424be8a33bae000131a3a3",
                       "customer_id":"5d424be8a33bae000131a3a2"
                   },
                   "target":"142855"
               },
               "title":{
                   "label":"测试找罗湖区餐馆",
                   "isAuthenticated":{
                       "label":"认证",
                       "value":false
                   }
               },
               "rent":"租金面议",
               "points":"12-20㎡",
               "info":"餐馆 | 罗湖区",
               "status":{
                   "label":"已跟进"
               }
           },
           {
               "matchtimes":"2019年10月10日",
               "times":"19-12-30",
               "link":{
                   "type":"page",
                   "params":{
                       "opportunity_id":"5dce46b65479c20001db5052",
                       "customer_id":"5dce1d445eddff0001001265"
                   },
                   "target":"142855"
               },
               "title":{
                   "label":"爱上找罗湖区餐馆",
                   "isAuthenticated":{
                       "label":"认证",
                       "value":false
                   }
               },
               "rent":"5000-6000元/月",
               "points":"12-122㎡",
               "info":"餐馆 | 蔡屋围",
               "status":{
                   "label":"未跟进"
               }
           }
       ] */
        // 数据重组
        this.tagData.data.data.forEach(element => {
            console.log(element.status.label)
            if (element.status.label === "未跟进") {
                element.status.stylestatus = "notfollow";
            } else if (element.status.label === "跟进中") {
                element.status.stylestatus = "following";
            } else if (element.status.label === "无意向") {
                element.status.stylestatus = "nointention";
            } else if (element.status.label === "已成交") {
                element.status.stylestatus = "traded";
            } else {
                element.status.stylestatus = "nullstyle";
            }
        });
    }
    getgo(item, obj?) {
        console.log(item)
        console.log(obj)
        if (obj) {
            const pageParams = {
                pageId: obj.target,
                params: obj.params,
                sessionId: localStorage.getItem('sessionId')
            };
            console.log(pageParams);
            this.appGlobalService.goToForward(pageParams);
        }
    }
    /* 上划获取翻页 */
    loadMoreData(event) {
        this.params = this.tagData.data.submit;
        // // 请求下一个页面
        this.pageNo = this.pageNo + 1;
        // // 参数
        this.params.content.pageNo = this.pageNo;
        this.params.sessionId = localStorage.getItem('sessionId');
        // 请求获取新数据
        this.http.post('application/submit', this.params, res => {
            // 请求数据成功
            if (res.success) {
                const resList = res.data.data;
                // 追加返回数据
                if (resList && resList.length > 0) {
                    // 追加数据
                    resList.map(v => {
                        this.tagData.data.data.push(v);
                    });
                } else {
                    event.target.disabled = true;
                    // 没有数据了
                    this.appGlobalService.showToast('没有数据了...', 1000, 'middle');
                }
            } else {
                // 请求失败，更新当前页数
                this.pageNo = this.pageNo - 1;
                // 请求数据失败
                this.appGlobalService.showToast(res.message, 2000, 'middle');
            }
            event.target.complete();
        }, err => {
            // 请求失败，更新当前页数
            this.pageNo = this.pageNo - 1;
            // 请求数据失败
            this.appGlobalService.showToast(err.message, 2000, 'middle');
            event.target.complete();

        });
    }
}
